home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8145 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.clark.net!usenet
  2. From: yom@clark.net (yom)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 28 Feb 1996 03:47:58 GMT
  6. Organization: Your Organization
  7. Message-ID: <4h0j9e$ng5@clarknet.clark.net>
  8. References: <177399702S86.JW1675A@american.edu>
  9. NNTP-Posting-Host: yom.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. Since you're trying to sort a char**, the qsort function call must
  15. look like this:
  16.  
  17. qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
  18.  
  19. And your compare function must be defined like this:
  20.  
  21. int compare(char **a,char **b) {return strcmp(*a,*b);}
  22.  
  23. The use of sizeof operator ensures that this code will be compatible
  24. with 64 bit architecture such as AXP/OSF.
  25.  
  26. Song (yom@clark.net)
  27.  
  28. In article <177399702S86.JW1675A@american.edu>, JW1675A@american.edu 
  29. says...
  30. >
  31. >Hi folks --
  32. >after reading the FAQ entries about qsort, I thought I had my problem
  33. >licked, but it seems not.
  34. >Architecture: SunOS 4.1.3 with ANSI-C compiler.
  35. >I'm doing some file manipulation and between steps A and B, I need to 
  36. sort.
  37. >For various reasons, I don't want to popen() to the sort utility--I 
  38. want to
  39. >use qsort.  Here's what I'm doing:
  40. >   get number of lines in the file
  41. >   (char**)malloc with enough room for all lines
  42. >   for each line in the file {
  43. >     (char*)malloc(90)  /* 90 is enough room for each line in file */
  44. >     copy each line into the newly malloc()ed space
  45. >     point a (char**) to the newly malloc()ed space
  46. >   }
  47. >so now I should have "lines" number of pointers to pointers to char,
  48. >each one pointing to 90 bytes containing a line in the file.
  49. >So, I call qsort(array[0], lines, 90, compare)
  50. >where compare is my comparison function -- prepared as discussed
  51. >in the FAQ.  Now all I get are core dumps during the call to qsort().
  52. >:-)
  53. >The core dump isn't in my compare routine -- it's somewhere else in
  54. >the qsort() function according to dbx -- so I'm sure it's a problem
  55. >setting up my data.
  56. >Any tips?  Much appreciation in advance,
  57. >Regards,
  58. >Jim
  59.  
  60.